﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Register_User : System.Web.UI.Page
{
    private SqlConnection connect;
    private SqlCommand command;
    private string connectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        connect = new SqlConnection(connectionString);
        connect.Open();

    }
    protected void getAddNewUser(string username, string pass, string full, string sex, string DOB, string address, string phone, string email)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }

            command = new SqlCommand("spAddnewUser", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = username;
            command.Parameters.Add("@UserPass", SqlDbType.VarChar).Value = pass;
            command.Parameters.Add("@UserFullName", SqlDbType.VarChar).Value = full;
            command.Parameters.Add("@UserSex", SqlDbType.VarChar).Value = sex;
            command.Parameters.Add("@UserBirthday", SqlDbType.VarChar).Value = DOB;
            command.Parameters.Add("@UserAddress", SqlDbType.VarChar).Value = address;
            command.Parameters.Add("@UserPhone", SqlDbType.VarChar).Value = phone;
            command.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = email;
            command.ExecuteNonQuery();
            lblStatus.Text = "Regis successfull ";
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }     
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        string user, pass, fullname, sex, dob, add, phone, email;
        user = txtUserName.Text;
        pass = txtPassword.Text;
        fullname = txtFullName.Text;
        if(RaMale.Checked == true)
        {
            sex = "Male";
        }
        else{
            sex = "Female";
        }
        dob = txtDOB.Text;
        add = txtAddress.Text;
        phone = txtPhone.Text;
        email = txtEmail.Text;
        getAddNewUser(user, pass, fullname, sex, dob, add, phone, email);
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtUserName.Text = "";
        txtPassword.Text = "";
        txtFullName.Text = "";
        txtDOB.Text = "";
        txtAddress.Text = "";
        txtPhone.Text = "";
        txtEmail.Text = "";
    }
}